package com.property.system.controller;

import com.property.common.annotation.Log;
import com.property.common.config.PropertyConfig;
import com.property.common.core.controller.BaseController;
import com.property.common.core.domain.AjaxResult;
import com.property.common.core.page.TableDataInfo;
import com.property.common.enums.BusinessType;
import com.property.common.exception.file.InvalidExtensionException;
import com.property.common.utils.file.FileUploadUtils;
import com.property.common.utils.file.MimeTypeUtils;
import com.property.system.domain.SysPropertyRepair;
import com.property.system.service.ISysPropertyRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 物业维修单控制器
 */
@RestController
@RequestMapping("/system/repair")
public class SysPropertyRepairController extends BaseController {

    @Autowired
    private ISysPropertyRepairService repairService;

    /**
     * 查询物业维修单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPropertyRepair repair) {
        startPage();
        List<SysPropertyRepair> list = repairService.selectSysPropertyRepairList(repair);
        return getDataTable(list);
    }

    /**
     * 查询处理完成的维修单（用于生成账单）
     */
    //@PreAuthorize("@ss.hasPermi('system:repair:list')")
    @GetMapping("/listDeal")
    public AjaxResult listDealRepairs() {
        List<SysPropertyRepair> list = repairService.selectDealSysPropertyRepairList();
        return success(list);
    }

    /**
     * 获取单个维修单详情
     */
    //@PreAuthorize("@ss.hasPermi('system:repair:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(repairService.selectSysPropertyRepairById(id));
    }

    /**
     * 新增维修单
     */
    //@PreAuthorize("@ss.hasPermi('system:repair:add')")
    @Log(title = "物业维修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPropertyRepair repair) {
        repair.setCreateBy(getUsername());
        return toAjax(repairService.insertSysPropertyRepair(repair));
    }

    /**
     * 维修单变更已处理
     */
   // @PreAuthorize("@ss.hasPermi('system:repair:edit')")
    @Log(title = "物业维修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPropertyRepair repair) {
        repair.setUpdateBy(getUsername());
        return toAjax(repairService.updateSysPropertyRepair(repair));
    }

    /**
     * 删除维修单（支持批量）
     */
    //@PreAuthorize("@ss.hasPermi('system:repair:remove')")
    @Log(title = "物业维修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(repairService.deleteSysPropertyRepairByIds(ids));
    }

    /**
     * 上传维修图片
     */
    @PostMapping("/uploadRepairImage")
    public AjaxResult uploadRepairImage(@RequestParam("file") MultipartFile file) throws IOException, InvalidExtensionException {
        if (!file.isEmpty()) {
            String imageUrl = FileUploadUtils.upload(PropertyConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", imageUrl);
            return ajax;
        }
        return error("上传图片失败，请联系管理员");
    }
}
